package top.hepingan.cloudorder.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.hepingan.cloudcommon.entity.Order;
import top.hepingan.cloudcommon.entity.User;
import top.hepingan.cloudcommon.mapper.OrderMapper;
import top.hepingan.cloudcommon.msg.Result;
import top.hepingan.cloudcommon.utils.UserUtils;
import top.hepingan.cloudorder.feign.AuthServiceFeign;
import top.hepingan.cloudorder.service.OrderService;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {

    private final OrderMapper orderMapper;

    private final AuthServiceFeign authServiceFeign;

    private final ObjectMapper objectMapper;

    @Override
    public void createOrder(Order order) {
        //使用feign
        Object data = authServiceFeign.getLoginUserDetail(UserUtils.getCurrentLoginId()).getData();
        User user = objectMapper.convertValue(data, User.class);
        order.setUserId(user.getUserId());
        order.setUsername(user.getUsername());
        order.setCreatedTime(LocalDateTime.now());
        orderMapper.insert(order);
    }
}
